﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TLECDataLayer
{
    public class Reservations
    {
        public static int CreateReservation(string custName, int numTickets, int eveId, DateTime purchDate, string shippingAdd)
        {
            if (!string.IsNullOrEmpty(custName) || !string.IsNullOrEmpty(shippingAdd) || purchDate == null || numTickets <= 0)
            {
                TLECDataDataContext context = new TLECDataDataContext();

                var eve =
                    (from e in context.Events
                     where e.EventId == eveId
                     select e).FirstOrDefault();

                if (eve != null)
                {
                    Reservation r = new Reservation()
                    {
                        CustomerName = custName,
                        NumberOfTickets = numTickets,
                        EventId = eveId,
                        PurchaseDate = purchDate,
                        ShippingAddress = shippingAdd,
                        StatusId = 1
                    };

                    context.Reservations.InsertOnSubmit(r);
                    context.SubmitChanges();

                    return r.ReservationId;
                }
                else
                {
                    throw new ArgumentException("Invalid event id");
                }
            }
            else
            {
                throw new ArgumentException("Invalid reservation arguments");
            }

        }

        public static IQueryable<Reservation> GetReservationsByStatus(int statId)
        {
            TLECDataDataContext context = new TLECDataDataContext();

            var reses =
                from r in context.Reservations
                where r.StatusId == statId
                orderby r.PurchaseDate descending
                select r;

            return reses;
        }

        public static Reservation GetReservationById(int resId)
        {
            TLECDataDataContext context = new TLECDataDataContext();

            var res =
                (from r in context.Reservations
                where r.ReservationId == resId
                select r).FirstOrDefault();

            return res;
        }

        public static void ChangeReservationStatus(int resId, int statId)
        {
            TLECDataDataContext context = new TLECDataDataContext();

            var res =
                (from r in context.Reservations
                 where r.ReservationId == resId
                 select r).FirstOrDefault();

            if (res != null)
            {
                res.StatusId = statId;
                context.SubmitChanges();
            }
            else
            {
                throw new ArgumentException("Invalid reservation id");
            }

        }

        public static IQueryable<Reservation> GetAllReservationsByEvent(int eveId)
        {
            TLECDataDataContext context = new TLECDataDataContext();

            var res =
                 from r in context.Reservations
                 where r.EventId == eveId
                 select r;

            return res;
        }

        public static IQueryable<Reservation> GetAllReservations()
        {
            TLECDataDataContext context = new TLECDataDataContext();

            return context.Reservations;
        }
    }
}
